#ifndef SVMEMORYPOOL_H
#define SVMEMORYPOOL_H

#include <QHash>
#include <QImage>
#include <QOBject>
#include <QSharedPointer>

#include "SVGobalSetting.h"

#define MEMPOOL SVMemoryPool::getInstance()

class SVMemoryPool : public QObject
{
    Q_OBJECT
public:
    SVMemoryPool(QObject *parent = nullptr);

    static SVMemoryPool *getInstance();
    void initialize(uint count, uint rows, uint cols, int type);
    cv::Mat convertQImageToCVImage(const QImage &image);
    QImage convertCVImageToQImage(const cv::Mat &mat);

    cv::Mat allocate(cv::Size size);
    void release(cv::Mat &mat);

private:
    QVector<cv::Mat> mMemoryPool;
    QVector<int> mFreeList;
    QHash<void *, int> mDataToIndex;
};

class SVScopedPool
{
public:
    SVScopedPool(SVMemoryPool *pool);
    ~SVScopedPool();
    cv::Mat convertQImageToCVImage(const QImage &image);

    cv::Mat allocate(cv::Size size);

private:
    SVMemoryPool *mPool;
    QVector<cv::Mat> mUsedMats;
};

#endif
